/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cratos.xxgramoss.group;

import com.cratos.xxgram.platf.base.BaseService;
import com.cratos.xxgram.platf.base.UserInfo;
import com.cratos.xxgram.platf.facade.OssService;
import com.cratos.xxgram.platf.group.GroupDetail;
import com.cratos.xxgram.platf.group.GroupDetailHis;
import com.cratos.xxgram.platf.group.UserGroup;
import com.cratos.xxgram.platf.message.BasicContent;
import com.cratos.xxgram.platf.message.MessageRecordHis;
import com.cratos.xxgram.platf.user.UserDetail;
import javax.annotation.Resource;
import org.redkale.net.http.RestService;
import org.redkale.source.*;
import org.redkale.util.*;
import static com.cratos.xxgramoss.base.Services.*;
import com.cratos.xxgramoss.message.MessageRecordHisBean;
import com.cratos.xxgramoss.user.UserOssService;
import java.util.ArrayList;
import java.util.List;
import org.redkale.net.http.*;
import org.redkale.service.RetResult;

/**
 *
 * @author alter
 */
@RestService(comment = "群组信息", moduleid = MODULE_GROUP)
public class GroupDetailOssService extends BaseService {

    @Resource(name = "platf")
    protected DataSource platfSource;
    
    @Resource
    protected UserOssService userService;
    
    @Resource
    protected OssService ossService;

    @RestMapping(auth = true, actionid = ACTION_QUERY, comment = "查询群组信息")
    public RetResult<Sheet<GroupDetail>> queryGroupList(GroupDetailBean bean, Flipper flipper) {
        Sheet<GroupDetail> sheet = new Sheet();
        short status = bean.getStatus();
        bean.setStatus((short)0);
        if(bean != null && status==GroupDetail.STATUS_NORMAL){
            sheet = platfSource.querySheet(GroupDetail.class, flipper, bean);
            sheet.forEach(group -> {group.setCreator(platfSource.find(UserDetail.class, group.getCreateuserid()));group.setStatus(GroupDetail.STATUS_NORMAL);});
        }else if(bean != null && status==GroupDetail.STATUS_DELTED){
            ArrayList<GroupDetail> list = new ArrayList<>();
            List<GroupDetailHis> his = platfSource.queryList(GroupDetailHis.class, flipper, bean);
            his.forEach(group -> {group.setCreator(platfSource.find(UserDetail.class, group.getCreateuserid()));group.setStatus(GroupDetail.STATUS_DELTED);});
            list.addAll(his);
            return new RetResult<>(new Sheet<>(list.size(), list));
        }else{
            ArrayList<GroupDetail> list = new ArrayList<>();
            int groupCount = source.getNumberResult(GroupDetail.class, FilterFunc.COUNT, 0, null, bean).intValue();
            int hisCount = source.getNumberResult(GroupDetailHis.class, FilterFunc.COUNT, 0, null, bean).intValue();
            if (groupCount + hisCount < 1) return new RetResult<>(new Sheet<>(0, list));
            if (groupCount > 0) {
                List<GroupDetail> group = source.queryList(GroupDetail.class, flipper, bean);
                group.forEach(obj -> obj.setStatus(GroupDetail.STATUS_NORMAL));
                list.addAll(group);
            }
            if (hisCount > 0) {
                List<GroupDetailHis> group = source.queryList(GroupDetailHis.class, flipper, bean);
                group.forEach(obj -> obj.setStatus(GroupDetailHis.STATUS_DELTED));
                list.addAll(group);
            }
            list.forEach(group -> group.setCreator(platfSource.find(UserDetail.class, group.getCreateuserid())));
            return new RetResult<>(new Sheet<>(groupCount + hisCount, list));
        }
        return new RetResult<>(sheet);
    }
    
    @RestMapping(auth = true, actionid = ACTION_UPDATE, comment = "解散群")
    public RetResult updateGroupStatus(int groupid) {
//        GroupDetail group = platfSource.find(GroupDetail.class, groupid);
//        group.createTableHis(System.currentTimeMillis());
//        platfSource.delete(GroupDetail.class, groupid);
        return ossService.dismissGroup(groupid);
    }

    @RestMapping(auth = true, actionid = ACTION_QUERY, comment = "查询群组成员")
    public RetResult<Sheet<UserGroup>> findGroupUser(UserGroupBean bean, Flipper flipper) {
        Sheet<UserGroup> group = platfSource.querySheet(UserGroup.class, flipper, bean);
        return new RetResult<>(group);
    }
    
    @RestMapping(auth = true, actionid = ACTION_UPDATE, comment = "移出群组")
    public RetResult removeUserFromGroup(int userid, int groupid) {
        UserInfo user = platfSource.find(UserDetail.class, userid);
        return ossService.removeUserFromGroup(user, groupid);
    }
    
    @RestMapping(auth = true, actionid = ACTION_QUERY, comment = "查询群组聊天记录")
    public RetResult<Sheet<MessageRecordHis>> findUserGroupMsg(MessageRecordHisBean bean, Flipper flipper, int groupid) {
        bean.setMsgkind(MessageRecordHis.MSG_KIND_GROUP);
        bean.setMsgtype(BasicContent.OBJ_IMAGE_MESSAGE);
        bean.setTargetid(groupid);
        Sheet<MessageRecordHis> sheet = platfSource.querySheet(MessageRecordHis.class, flipper, bean);
        sheet.forEach(msg -> msg.setFromname(userService.findUserName(msg.getFromid())));
        return new RetResult<>(sheet);
    }
    
    public String findGroupName(int groupid) {
        GroupDetail group = platfSource.find(GroupDetail.class, groupid);
        if (group == null) {
            return "";
        }
        return group.getGroupname();
    }
    
}
